#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

int fib(int n)
{
  if(n<=1)
    return n;
  int a=0,b=1,c;
  int i;
  //loop invariant: a=fib(i-2), b=fib(i-1), i in[2,n]
  //initialization: i=2; a=fib(0); b=fib(1)
  for(i = 2; i<=n; ++i){
    c = a+b;
    a = b;
    b = c;
  }
  return c;//or b
}

int main(int argc, char *argv[])
{
  int n;
  //initialize n
  if(argc == 2){
    n = atoi(argv[1]);
  }
  else{
    puts("usage: fib n");
    return 1;
  }
  assert(n>=0);

  printf("fib(%d) is %d\n",n, fib(n) );
  return 0;
}
